package com.jiuzhou.guanwang.jzcp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.adapter.AwardItemAdapter;
import com.jiuzhou.guanwang.jzcp.base.BaseActivity;
import com.jiuzhou.guanwang.jzcp.base.DefaultAdapter;
import com.jiuzhou.guanwang.jzcp.bean.HomeCPBean;
import com.jiuzhou.guanwang.jzcp.bean.OpenResult;
import com.jiuzhou.guanwang.jzcp.presenter.PastOpenPresenter;
import com.jiuzhou.guanwang.jzcp.utils.CommonUtils;
import com.jiuzhou.guanwang.jzcp.view.PastOpenView;
import com.jiuzhou.guanwang.jzcp.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PastOpenActivity extends BaseActivity<PastOpenPresenter> implements PastOpenView {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private String mCode;
    private ArrayList<HomeCPBean> mData = new ArrayList<>();
    private String mName;
    private AwardItemAdapter mOpenAdapter;
    private String[] support_lotterys = {"ssq", "dlt", "fc3d", "pl3", "pl5", "qxc", "qlc", "js11x5", "jsk3"};
    @Override
    protected PastOpenPresenter initPresenter() {
        return new PastOpenPresenter(this);
    }

    @Override
    protected void initialize() {

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding(R.layout.activity_past_open);
        Intent intent = getIntent();
        this.mCode = intent.getStringExtra("code");
        this.mName = intent.getStringExtra("name");
        initView();
        initData();
    }

    private void initView() {
        title .setText(mName);
        mOpenAdapter = new AwardItemAdapter(mContext, mData);
        recyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.divider_list)));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mOpenAdapter);
        mOpenAdapter.setOnItemClickListener(new DefaultAdapter.OnItemClickListener<HomeCPBean>() {
            @Override
            public void onClick(View view, RecyclerView.ViewHolder holder, HomeCPBean item, int position) {
//                Intent intent = new Intent(mContext, LastOpenActivity.class);
//                intent.putExtra("name", item.getName());
//                intent.putExtra("code", item.getCode());
//                startActivity(intent);
            }
        });
    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        this.mName = intent.getStringExtra("name");
        this.mCode = intent.getStringExtra("code");
    }

    private void initData() {
        presenter.getData(mCode);
    }

    @Override
    public void dismissLoadingDialog() {
        getLoadingProgressDialog().dismiss();
    }

    @Override
    public void showLoadingDialog() {
        getLoadingProgressDialog().show();
    }

    @Override
    public void onDataSuccess(OpenResult openResult) {
        if (openResult != null && openResult.showapi_res_code == 0 && openResult.showapi_res_body != null
                && openResult.showapi_res_body.getRet_code() == 0
                && !CommonUtils.isEmpty(openResult.showapi_res_body.getResult())) {
            mData.clear();
            for (HomeCPBean openBean : openResult.showapi_res_body.getResult()) {
                if (!(openBean == null || TextUtils.isEmpty(openBean.code))) {
                    List<String> lotterys = Arrays.asList(support_lotterys);
                    if (lotterys.contains(openBean.code)) {
                        mData.add(openBean);
                    }
                }
            }
            mOpenAdapter.notifyDataSetChanged();
        }
    }

    @OnClick(R.id.tv_title_right)
    public void onViewClicked() {
    }
}